<#include "/WEB-INF/views/admin/inc/header.ftl" />
<#import "/META-INF/spring.ftl" as spring />
<#import "/META-INF/mspring.ftl" as mspring />
<div id="error" class="message error" style="display:none;"></div>
<form class="form" id="ruleForm" action="${base}/admin/spider/rule/edit/save" method="POST">
	<@spring.bind "spiderRule" />
	<@spring.formHiddenInput path="spiderRule.id" />
	<@spring.formHiddenInput path="spiderRule.status" />
	<table class="infotable">
		<tr>
			<td class="fieldlabel" style="width:80px;">名称</td>
			<td>
				<@spring.formInput path="spiderRule.name" attributes='class="textinput" style="width:98%;" validate=\'{required: true, messages:{required:"规则标题"}}\'' />
			</td>
			<td style="width:150px;">
			</td>
		</tr>
		<tr>
			<td class="fieldlabel" style="width:80px;">启用</td>
			<td>
				<@spring.formRadioButtons path="spiderRule.enabled" options=enabled defaultValue="false" separator="&nbsp;" />
			</td>
			<td style="width:150px;">
			</td>
		</tr>
		<tr>
			<td colspan="3" class="partition">第一步：列表规则</td>
		</tr>
		<tr>
			<td class="fieldlabel" style="width:80px;">列表页URL</td>
			<td>
				<@spring.formInput path="spiderRule.listUrl" attributes='class="textinput" style="width:98%;" validate=\'{required: true, messages:{required:"请输入URL"}}\'' />
			</td>
			<td>
				<span style="color:red">分页占位符：$PAGE$</span>
			</td>
		</tr>
		<tr>
			<td class="fieldlabel" style="width:80px;">列表起始页</td>
			<td>
				<@spring.formInput path="spiderRule.startPage" attributes='class="textinput" style="width:98%;" validate=\'{required: true, digits: true, messages:{required:"请输入列表起始页", digits:"起始页必须为数字"}}\'' />
			</td>
			<td>
			</td>
		</tr>
		<tr>
			<td class="fieldlabel" style="width:80px;">列表结束页</td>
			<td>
				<@spring.formInput path="spiderRule.endPage" attributes='class="textinput" style="width:98%;" validate=\'{required: true, digits: true, messages:{required:"请输入列表结束页", digits:"结束页必须为数字"}}\'' />
			</td>
			<td>
			</td>
		</tr>
		<tr>
			<td class="fieldlabel" style="width:80px;">列表规则</td>
			<td>
				<@spring.formInput path="spiderRule.listRule" attributes='class="textinput" style="width:98%;" validate=\'{required: true, messages:{required:"请输入列表规则"}}\'' />
			</td>
			<td>
			</td>
		</tr>
		<tr>
			<td class="fieldlabel" style="width:80px;">测试地址</td>
			<td>
				<input name="testListURL" id="testListURL" type="text" class="textinput" style="width:98%;"/>
			</td>
			<td>
				<input type="button" class="btn" value="测试" onclick="testUrllist()"/>
			</td>
		</tr>
		<tr>
			<td colspan="3" class="partition">第二步：内容采集规则</td>
		</tr>
		<tr>
			<td class="fieldlabel" style="width:80px;">标题规则</td>
			<td>
				<@spring.formInput path="spiderRule.titleRule" attributes='class="textinput" style="width:98%;" validate=\'{required: true, messages:{required:"请输入正文规则"}}\'' />
			</td>
			<td>
			</td>
		</tr>
		<tr>
			<td class="fieldlabel" style="width:80px;">正文规则</td>
			<td>
				<@spring.formInput path="spiderRule.contentRule" attributes='class="textinput" style="width:98%;" validate=\'{required: true, messages:{required:"请输入正文规则"}}\'' />
			</td>
			<td>
			</td>
		</tr>
		<tr>
			<td class="fieldlabel" style="width:80px;">测试地址</td>
			<td>
				<input name="testContentURL" id="testContentURL" type="text" class="textinput" style="width:98%;"/>
			</td>
			<td>
				<input type="button" class="btn" value="测试" onclick="testContent();"/>
			</td>
		</tr>
		<tr>
			<td colspan="3" class="partition">第三步：采集配置</td>
		</tr>
		<tr>
			<td class="fieldlabel" style="width:80px;">采集类</td>
			<td>
				<@spring.formInput path="spiderRule.spiderClass" attributes='class="textinput" style="width:98%;" validate=\'{required: true, messages:{required:"请输入采集类"}}\'' />
			</td>
			<td>
			</td>
		</tr>
		<tr>
			<td class="fieldlabel" style="width:80px;">采集时间间隔</td>
			<td>
				<@spring.formInput path="spiderRule.interval" attributes='class="textinput" style="width:98%;" validate=\'{required: true, digits: true, messages:{required:"请输入采集时间间隔", digits:"采集时间间隔必须为数字"}}\'' />
			</td>
			<td>
			</td>
		</tr>
		<tr>
			<td colspan="3" style="text-align:center;">
				<input type="submit" class="btn" value=" 提交 " />
			</td>
		</tr>
	</table>
</form>

<script type="text/javascript">
	turnHighLight(815005015);
	
	function testUrllist(){
		var testListURL = document.getElementById('testListURL').value;
		var listRule = document.getElementById('listRule').value;
		if(!testListURL){
			alert('请填入测试地址');
			return;
		}
		if(!listRule){
			alert('请填入列表规则');
			return;
		}
		$.post('${base}/admin/spider/rule/test/urllist', {testListURL : testListURL, listRule: listRule}, function(response){
			if(response.success){
				var content = '';
				if(response.data && response.data.urls){
					for(var i = 0; i < response.data.urls.length; i++){
						content += response.data.urls[i] + '<br/>';
					}
				}
				mlog.dialog.showModelDialog({
					title : '列表测试结果',
					content : content
				}); 
			}
			else {
				alert(response.message);
			}
		});
	}
	
	function testContent(){
		var testContentURL = document.getElementById('testContentURL').value;
		var titleRule = document.getElementById('titleRule').value;
		var contentRule = document.getElementById('contentRule').value;
		if(!testContentURL){
			alert('请填入测试地址');
			return;
		}
		if(!titleRule){
			alert('请填入标题规则');
			return;
		}
		if(!contentRule){
			alert('请填入内容规则');
			return;
		}
		$.post('${base}/admin/spider/rule/test/content', {testContentURL : testContentURL, titleRule: titleRule, contentRule: contentRule}, function(response){
			if(response.success){
				var title = response.data.title;
				var content = response.data.content;
				var html = [
					'<div style="max-height:500px;max-width:800px;overflow:scroll;">',
					'<h2>' + title + '</h2><hr/>',
					'<div>' + content + '</div>',
					'</div>',
				].join('');
				mlog.dialog.showModelDialog({
					title : '内容测试结果',
					content : html
				}); 
			}
			else {
				alert(response.message);
			}
		});
	}
	
	$(document).ready(function(){
		//斑马线
		var tables=document.getElementsByTagName("table");
		var b=false;
		for (var j = 0; j < tables.length; j++){
			var cells = tables[j].getElementsByTagName("tr");
			//cells[0].className="color3";
			b=false;
			for (var i = 0; i < cells.length; i++){
				if(b){
					cells[i].className="color2";
					b=false;
				}
				else{
					cells[i].className="color3";
					b=true;
				};
			};
		}
		
		mlog.form.validate({
			selector : "#ruleForm",
			errorLabelContainer : "#error",
			wrapper: 'li',
			onfocusout : false,
			onkeyup : false,
			onclick : false,
			success : function(){
				mlog.utils.scrollTop();
			}
		});
		
	});
</script>
<#include "/WEB-INF/views/admin/inc/footer.ftl" />